package com.situ.stl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stl.dao.MemberMapper;
import com.situ.stl.dao.ReserveMapper;
import com.situ.stl.entity.Member;
import com.situ.stl.entity.Reserve;
import com.situ.stl.service.MemberService;
import java.util.List;

import com.situ.stl.util.MD5Util;
import com.situ.stl.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/** 
 * Service层 - 会员
 */
@Service 
public class MemberServiceImpl implements MemberService{

    /** 
     */
    @Autowired 
    private MemberMapper memberMapper;
    @Autowired
    private ReserveMapper reserveMapper;

    /** 
     * add
     */
    @Override 
    public int add( Member member) throws Exception {
        Member sMember = memberMapper.selectByPhone(member.getPhone());
        if ( ! ObjectUtils.isEmpty(sMember)) {
            throw new Exception("手机号已存在！");
        }

        // 密码加密
        member.setPassword(MD5Util.getMD5(member.getPassword()));

        int res = memberMapper.insert(member);
        return res;
    }

    /** 
     * remove
     */
    @Override 
    public int remove( int id) throws Exception {
        Member sMember = memberMapper.selectByPrimaryKey(id);
        if ( ObjectUtils.isEmpty(sMember)) {
            throw new Exception("会员不存在！");
        }

        List<Reserve> reserves = reserveMapper.selectByMember(id);
        if ( ! reserves.isEmpty()){
            throw new Exception("会员有课，不能删除！");
        }

        int res = memberMapper.delete(id);
        return res;
    }

    /** 
     * edit
     */
    @Override 
    public int edit( Member member) throws Exception {
        Member sMember = memberMapper.selectByPhone(member.getPhone());
        if ( ! ObjectUtils.isEmpty(sMember) && !sMember.getId().equals(member.getId())) {
            throw new Exception("手机号已存在！");
        }

        // 密码加密
        if (StringUtils.hasText(member.getPassword())) {
            member.setPassword(MD5Util.getMD5(member.getPassword()));
        }

        int res = memberMapper.update(member);
        return res;
    }

    /** 
     * getById
     */
    @Override 
    public Member getById( int id) {
        return memberMapper.selectByPrimaryKey(id);
    }

    @Override
    public Member getByPhone(String phone) {
        return memberMapper.selectByPhone(phone);
    }

    /** 
     * get All
     */
    @Override 
    public List<Member> getAll() {
        return memberMapper.selectAll();
    }

    /** 
     * search
     */
    @Override 
    public List<Member> search( Member member) {
        return memberMapper.select(member);
    }

    /** 
     * searchByPage
     */
    @Override 
    public PageInfo searchByPage( Integer page, Integer limit, Member member) {
        if (page == null) page = 1;
        if (limit == null) limit = 10;
        
        PageHelper.startPage(page, limit);
        List list = memberMapper.select(member);
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 前台登录
     * @param member
     * @return
     * @throws Exception
     */
    @Override
    public Member login(MemberVo member) throws Exception {
        Member loginMember = memberMapper.selectByPhone(member.getPhone());
        if (ObjectUtils.isEmpty(loginMember)){
            throw new Exception("手机号不存在！");
        }

        String md5Password = MD5Util.getMD5(member.getPassword());
        if (!md5Password.equals(loginMember.getPassword())){
            throw new Exception("密码不正确");
        }

        return loginMember;
    }

    /**
     * 重置密码
     * @param member
     * @return
     */
    @Override
    public int resetPwd(MemberVo member) throws Exception{
        return 0;
    }

}